<template>
	<u-form :model="form" ref="uForm">
		<u-form-item label="类型"  :label-style="{color:'white'}" label-width="120rpx" ><u-input class="input" v-model="form.type"  type="select"  @click="show = true"  />
    <u-select v-model="show" :list="typeList" value-name="id" label-name="name"  @confirm="confirm"></u-select>
    </u-form-item>
    <u-form-item label="名称" :label-style="{color:'white'}" label-width="120rpx" prop="name"><u-input class="input" v-model="form.name"  type="select"  @click="nameShow = true"  />
    <u-select v-model="nameShow" :list="nameList" value-name="id" label-name="name"  @confirm="nameConfirm"></u-select>
    </u-form-item>
    <u-form-item label="货架" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input" v-model="form.shelves"  type="select"  @click="shelvesShow = true"  />
    <u-select v-model="shelvesShow" :list="shelvesList" value-name="id" label-name="shelfname"  @confirm="shelvesConfirm"></u-select>
    </u-form-item>
    <u-form-item label="单位" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input" v-model="form.unit"  type="select"  @click="unitShow = true"  />
    <u-select v-model="unitShow" :list="unitList" value-name="id" label-name="unitname"  @confirm="unitConfirm"></u-select>
    </u-form-item>
    <u-form-item label="来源" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input" v-model="form.source"  type="select"  @click="sourceShow = true"  />
    <u-select v-model="sourceShow" :list="sourceList" value-name="id" label-name="sourcename"  @confirm="sourceConfirm"></u-select>
    </u-form-item>
    <u-form-item label="配发时间" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input" v-model="form.allotmentTime"  type="select"  @click="withTimeShow=true"/>
    <u-picker v-model="withTimeShow" :params="timeParams" mode="time" @confirm="confirmAllotTime($event)"></u-picker>
    </u-form-item>
    <u-form-item label="过期时间" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input"  v-model="form.expirationTime"  type="select"  @click="expirationTimeShow = true"  />
    <u-picker v-model="expirationTimeShow" :params="timeParams" mode="time" @confirm="expirationTimeConfirm($event)"></u-picker>
    </u-form-item>
    <u-form-item label="RFID" :label-style="{color:'white'}" label-width="120rpx"><u-input class="input" placeholder="扫描获取" v-model="RFID"/></u-form-item>
    <view class="buttonTop">
      <view>
        <u-button type="primary" size="medium" @click="confirmInventory">确定入库</u-button>
      </view>
      <view>
        <u-button type="primary" size="medium" @click="removeRfid">清除RFID</u-button>
      </view>
    </view>
      <u-toast ref="uToast"></u-toast>
	</u-form>
</template>

<script>
// 导入sdkwx插件
import sqlLite from '../../common/sqllite-utils.js';
import { confirmInventory } from './incoming-information.js';

const sdkwx = uni.requireNativePlugin('GGRfid-SdkWX');

export default {
  data() {
    return {
      RFID: '',
      typeid: '',
      // nameid,用作请求名称的一个参数
      nameid: '',
      form: {
        type: '',
        name: '',
        shelves: '',
        unit: '',
        source: '',
        allotmentTime: '',
        expirationTime: '',
      },
      show: false,
      nameShow: false,
      shelvesShow: false,
      unitShow: false,
      sourceShow: false,
      expirationTimeShow: false,
      // 配置需要显示的参数
      timeParams: {
        year: true,
        month: true,
        day: true,
        hour: true,
        minute: true,
        second: false,
      },
      // 配发时间弹窗的显示与隐藏
      withTimeShow: false,
      allotVal: '',
      overdueAllotVal: '',
      sourceval: '',
      deptnameVal: '',
      shelfCode: '',
      rules: {
        			name: [
        					{
        						required: true,
        						message: '请输入名字',
        						// 可以单个或者同时写两个触发验证方式
        						trigger: ['change', 'blur'],
        					},
        				],
        			},
    };
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
    this.$refs.uToast.show({
      title: '开启扫描成功',
      type: 'success',
    });
    const readerName = '/dev/ttysWK0:115200';
    const packageSize = 64;
    const freeWait = 100;
    const power = 15;

    sdkwx.open(readerName, packageSize, freeWait, power);
    const that = this;
    // 设置回调事件
    sdkwx.setTagEpcCallback((res) => {
      that.RFID = res.log.epc;
    });
  },
  onUnload() {
    sdkwx.close();
  },
  onLoad() {
    // this.shiyouid = uni.getStorageSync('shiyouid')
  },
  methods: {
    confirm(e) {
      console.log(e, '选定的值');
      // 接口传入值
      this.typeid = e[0].value;
      const mediationArr = this.namelist.filter((item) => item.hb_rock_zhuang_fen_lei_id == this.typeid);
      this.form.type = e[0].label;
      this.$u.vuex('nameList', mediationArr);
    },
    nameConfirm(e) {
      this.form.name = e[0].label;
      this.nameid = e[0].value;
    },
    shelvesConfirm(e) {
      this.form.shelves = e[0].label;
      this.shelfCode = e[0].value;
    },
    unitConfirm(e) {
      this.deptnameVal = e[0].value;
      this.form.unit = e[0].label;
    },
    sourceConfirm(e) {
      this.sourceval = e[0].value;
      this.form.source = e[0].label;
    },
    expirationTimeConfirm(e) {
      this.form.expirationTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`;
      this.overdueAllotVal = new Date(e.year, e.month - 1, e.day, e.hour, e.minute);
      this.overdueAllotVal = this.overdueAllotVal.toISOString();
    },
    /**
     * 选定配发时间
     */
    confirmAllotTime(e) {
      console.log(e);
      this.form.allotmentTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`;
      this.allotVal = new Date(e.year, e.month - 1, e.day, e.hour, e.minute);
      this.allotVal = this.allotVal.toISOString();
    },
    /**
     * 确认入库
     */
    confirmInventory() {
      confirmInventory.call(this);
    },
    /**
     * 清除rfid
     */
    removeRfid() {
      this.RFID = '';
    },
  },
};
</script>
<style lang="scss">
.buttonTop {
  display: flex;
  justify-content: space-around;
  margin-top: 30rpx;
}
.buttonBottom {
  display: flex;
  justify-content: space-around;
  margin-top: 30rpx;
}
.kushi {
  text-align: center;
  line-height: 150rpx;
  font-size: 40rpx;
  color: white;
}

 /deep/ .input{
	.u-input__input{
	 color: #fff!important;
			}
	}
</style>
